package com.asa.photopicker.photo;

import android.os.Process;

/**
 * 有优先级别的任务
 */
public class TaskWithPriority implements Comparable<TaskWithPriority>, Runnable {

	private int mPriority;
	private Runnable mRunnable;
	private int mOrder;

	public TaskWithPriority(Runnable runnable, int level) {
		mRunnable = runnable;
		mPriority = level;
	}

	public TaskWithPriority(Runnable runnable, int level, int order) {
		mRunnable = runnable;
		mPriority = level;
		mOrder = order;
	}

	@Override
	public void run() {
		// 设置后台线程的优先级
		Process.setThreadPriority(Process.myTid(), Process.THREAD_PRIORITY_BACKGROUND);
		mRunnable.run();
	}

	@Override
	public int compareTo(TaskWithPriority o) {
		// 降序排列
		return -(mPriority - o.mPriority);
	}

	@Override
	public String toString() {
		return "priority:" + mPriority + ";order:" + mOrder;
	}
}